---@class VoidGame.GameLogic : Base.GameLogic
local GameLogic = Fk.Base.GameLogic:subclass("Battlegrounds.GameLogic")



function GameLogic:run()
  self:adjustSeats()
  self:assignRole()

  self:initBoard()

  self:action()
end


function GameLogic:assignRole()
  local room = self.room
end

---@param board integer[][]?
function GameLogic:initBoard(board)
  local room = self.room
  
  -- 初始化酒馆战棋游戏状态
  room.turn = 1
  room.gamePhase = room.RecruitPhase
  
  -- 为每个玩家初始化游戏状态

  for i, player in ipairs(room.players) do
    -- 初始化玩家基础状态
   
    player.gold = 3
    player.hp = 40
    player.shield = 0
    player.frozen = false
    player.eliminated = false
    player.rank = i
    
    -- 初始化玩家区域
    player.board = {}
    player.hand = {}


    player.tavern_level = 1 --商店等级
    player.shop = {}--商店里的货物
    
    -- 生成初始商店
    --player:generateShop()
  end
  
  -- 发送游戏初始化完成日志
  room:sendLog{
    type = "#BattlegroundsInitComplete",
    arg = #room.players,
  }
end

function GameLogic:turn(count)
  local room = self.room
  
  -- 更新房间回合数
  room.turn = count
  
  -- 检查游戏是否应该结束
  if self:checkGameEnd() then
    return
  end
  
  -- 开始招募阶段
  self:startRecruitPhase()
end

--- 开始招募阶段
function GameLogic:startRecruitPhase()
  local room = self.room
  local players = room.players
  -- 设置游戏阶段为招募阶段
  --room:setGamePhase(room.RecruitPhase)
  
  -- 给所有存活玩家发放金币
  local req = Request:new(players, "Battlegrounds.RecruitPhase")
  for _, player in ipairs(players) do
      local shopcards = room:getShopCards(player, 2)
      local data = {shopcards=shopcards}
      req:setData(player, data)
      req:setDefaultReply(player, "123")
  end
  dbg()
  req:getResult(players[1])
  dbg()
end

--- 给玩家发放金币
function GameLogic:giveGoldToPlayers()
  local room = self.room
  
  for _, player in ipairs(room.players) do
    if not player:isDead() then
      -- 基础金币：回合数 + 2
      local baseGold = room.turn + 2
      
      -- 酒馆等级奖励：每级额外1金币
      local tavernBonus = player.tavern_level - 1
      
      -- 总金币
      local totalGold = baseGold + tavernBonus
      
      player.gold = player.gold + totalGold
      
      -- 发送金币获得日志
      room:sendLog {
        type = "#BattlegroundsGoldGained",
        arg = player.id,
        arg2 = totalGold,
        arg3 = player.gold,
      }
    end
  end
end

--- 刷新玩家商店
function GameLogic:refreshPlayerShop(player)
  local room = self.room
  
  -- 如果不是冻结状态，刷新商店
  if not player.frozen then
    player:generateShop()
    
    -- 发送商店刷新日志
    room:sendLog {
      type = "#BattlegroundsShopRefreshed",
      arg = player.id,
    }
  else
    -- 解冻商店
    player.frozen = false
    
    room:sendLog {
      type = "#BattlegroundsShopUnfrozen",
      arg = player.id,
    }
  end
end



--- 处理玩家招募阶段的操作
function GameLogic:processPlayerRecruitActions(player, actions)
  local room = self.room
  
  -- 这里可以处理玩家在招募阶段的各种操作
  -- 比如购买随从、升级酒馆、刷新商店等
  -- 具体实现可以根据actions的内容来处理
  
  room:sendLog {
    type = "#BattlegroundsPlayerRecruitComplete",
    arg = player.id,
  }
end

--- 检查游戏是否应该结束
function GameLogic:checkGameEnd()
  local room = self.room
  local alivePlayers = {}
  
  -- 统计存活玩家
  for _, player in ipairs(room.players) do
    if not player:isDead() then
      table.insert(alivePlayers, player)
    end
  end
  
  -- 如果只剩一个玩家，游戏结束
  if #alivePlayers <= 1 then
    self:endGame(alivePlayers[1])
    return true
  end
  
  return false
end

--- 结束游戏
function GameLogic:endGame(winner)
  local room = self.room
  
  room:sendLog {
    type = "#BattlegroundsGameEnd",
    arg = winner and winner.id or 0,
  }
end

function GameLogic:action()
  local room = self.room
  
  -- 开始游戏循环
  local turnCount = 1
  while not self:checkGameEnd() do
    self:turn(turnCount)
    turnCount = turnCount + 1
    
    -- 防止无限循环，最多进行100回合
    if turnCount > 100 then
      break
    end
  end
end

return GameLogic
